package ihm.views.main;

import ihm.actions.CloseFrameAction;
import ihm.actions.OpenFrame;
import ihm.actions.files.NewAction;
import ihm.actions.files.OpenAction;
import ihm.actions.files.SaveAction;
import ihm.controllers.ATKcontroller;
import ihm.models.ATKlistener;
import ihm.models.events.CharacterChangedEvent;
import ihm.models.events.EquipmentChangedEvent;
import ihm.models.events.ItemTypeChangedEvent;
import ihm.models.events.LanguageChangedEvent;
import ihm.models.events.QueryChangedEvent;
import ihm.models.events.StatChangedEvent;
import ihm.views.preferences.LanguageBox;

import java.awt.Container;
import java.util.List;

import javax.swing.JMenuBar;

import systems.jMenuBuilder.MenuBuilder;
import systems.jMenuBuilder.elements.items.MenuItem;
import systems.jMenuBuilder.elements.menus.Menu;

/**
 * This class contains the Menu Bar (a JMenuBar) for the main frame.
 * @author Baptiste Legrand & Benjamin Orman
 * @version 2.0
 */
public class MainMenuBar implements ATKlistener
{
  //The JMenuBar instance.
  private JMenuBar menuBar;
  //The list of the different menu contains in the MenuBar
  private List<Menu> menuList; 
  //The parent container which will contains the MenuBar
  private Container parent;
  /**
   * Constructor creates the MainMenuBar.
   * This constructor uses the MenuBuilder to create the JMenuBar and copie the
   * list of all the menu required to create the menu.
   */
  public MainMenuBar(ATKcontroller controller, Container parent)
  {
    this.parent = parent;
    //Create a MenuBuilder instance.
    MenuBuilder.getMenuBuilderInstance();
    this.menuBar = MenuBuilder.getMenuBar();
    this.menuList = MenuBuilder.getMenuList();
    //treat actions
    this.setActions(controller);
  }

  /**
   * Returns the JMenuBar created. 
   * @return JMenuBar - The JMenuBar created
   */
  public JMenuBar getJMenuBar()
  {
    return this.menuBar;
  }

  /**
   * This method links the different actions associate to all menu.
   */
  private void setActions(ATKcontroller controller)
  {
    //File Menu Action
    setNewAction(controller);
    setOpenAction(controller);
    setSaveAction(controller);
    setSaveAsAction(controller);
    setQuitAction();
    //Preference Menu Action   
    setDatabaseAction();
    setLanguageAction(controller);
    //Help Menu Action
    setAboutAction();
  }

  //File Menu Action
  /**
   * Creates the action associate to the menu named "New"
   */
  private void setNewAction(ATKcontroller controller)
  {
    //this.menuList.get(0) -> "File" Menu
    //getAllItems() -> All menus contain in the File menu
    //get(0) -> get The "New" Menu
    MenuItem menuNew= (MenuItem) this.menuList.get(0).getAllItems().get(0);
    menuNew.addActionListener(new NewAction(controller));
  }
  /**
   * Creates the action associate to the menu named "Open"
   */
  private void setOpenAction(ATKcontroller controller)
  { 
    //this.menuList.get(0) -> "File" Menu
    //getAllItems() -> All menus contain in the File menu
    //get(1) -> get The "Open" Menu
    MenuItem menuOpen= (MenuItem) this.menuList.get(0).getAllItems().get(1);
    menuOpen.addActionListener(new OpenAction(controller));
  }
  /**
   * Creates the action associate to the menu named "Save"
   */
  private void setSaveAction(ATKcontroller controller)
  {
    //this.menuList.get(0) -> "File" Menu
    //getAllItems() -> All menus contain in the File menu
    //get(1) -> get The "Save" Menu
    MenuItem menuOpen= (MenuItem) this.menuList.get(0).getAllItems().get(2);
    menuOpen.addActionListener(new SaveAction(controller));
  }
  /**
   * Creates the action associate to the menu named "SaveAs"
   */
  private void setSaveAsAction(ATKcontroller controller)
  {
    //this.menuList.get(0) -> "File" Menu
    //getAllItems() -> All menus contain in the File menu
    //get(2) -> get The "Save" Menu
    MenuItem menuOpen= (MenuItem) this.menuList.get(0).getAllItems().get(3);
    menuOpen.addActionListener(new SaveAction(controller));
  }
  /**
   * Creates the action associate to the menu named "Quit"
   */
  private void setQuitAction()
  {
    //this.menuList.get(0) -> "File" Menu
    //getAllItems() -> All menus contain in the File menu
    //get(3) -> get The "Save" Menu
    MenuItem menuOpen= (MenuItem) this.menuList.get(0).getAllItems().get(4);
    menuOpen.addActionListener(new CloseFrameAction(this.parent));
  }

  /**
   * Creates the action associate to the menu named "Database configuration"
   */
  private void setDatabaseAction()
  {
    MenuItem menuDatabase = (MenuItem) this.menuList.get(1).getAllItems().get(0);
    menuDatabase.addActionListener(new OpenFrame("ihm.views.preferences.DataBaseBox"));
  }
  /**
   * Creates the action associate to the menu named "Language"
   */
  private void setLanguageAction(ATKcontroller controller)
  {
    MenuItem menuLanguage= (MenuItem) this.menuList.get(1).getAllItems().get(1);
    menuLanguage.addActionListener( new LanguageBox(controller.getModel()));
  }
  /**
   * Creates the action associate to the menu named "About"
   */
  private void setAboutAction()
  {
    MenuItem menuAbout= (MenuItem) this.menuList.get(2).getAllItems().get(0);
    menuAbout.addActionListener( new OpenFrame("ihm.views.help.AboutBox"));
  }

  @Override
  public void CharacterChanged(CharacterChangedEvent event) {
    //Nothing to do
  }

  @Override
  public void EquipmentChanged(EquipmentChangedEvent event) {
    //Nothing to do
  }

  @Override
  public void ItemTypeChanged(ItemTypeChangedEvent event) {
    //Nothing to do
  }

  @Override
  public void LanguageChanged(LanguageChangedEvent event) {
    MenuBuilder.updateMenus();
  }

  @Override
  public void QueryChanged(QueryChangedEvent event) {
    //Nothing to do 
  }

  @Override
  public void StatChanged(StatChangedEvent event) {
    //Nothing to do
  }
}
